const STORAGE_KEY = 'favoriteQuestions';

Page({
  data: {
    questions: [],            // 收藏题列表
    currentIndex: 0,
    userAnswer: '',
    answeredQuestions: [],
    showResult: false,
    showNavigator: false
  },

  onLoad() {
    // 从本地读取收藏列表
    const favs = wx.getStorageSync(STORAGE_KEY) || [];
    // 为每道题初始化 userAnswer、optionsArray.statusClass、collected
    const questions = favs.map(q => ({
      ...q,
      userAnswer: q.userAnswer || '',
      collected: true,
      // 初始化每个选项的 statusClass
      optionsArray: (q.optionsArray || []).map(opt => ({
        ...opt,
        statusClass: ''
      }))
    }));
    this.setData({ questions });
  },

  // 切换收藏：从列表中移除当前题
  toggleCollect() {
    const idx = this.data.currentIndex;
    let qs = this.data.questions;
    qs.splice(idx, 1);
    this.setData({ questions: qs });

    wx.setStorageSync(STORAGE_KEY, qs);
    if (qs.length === 0) {
      wx.showToast({ title: '已取消所有收藏', icon: 'none' });
      wx.navigateBack();
    } else if (idx >= qs.length) {
      // 如果删到最后一题，索引回退
      this.setData({ currentIndex: qs.length - 1 });
    }
  },

  // 单选题选中
  onSelect(e) {
    const uniqId = e.currentTarget.dataset.uniqid;
    const key    = e.currentTarget.dataset.key;
    const idx    = this.data.questions.findIndex(q => q.uniqId === uniqId);
    if (idx < 0) return;

    const q = this.data.questions[idx];
    if (q.userAnswer) return; // 已答过

    q.userAnswer = key;
    const correct = key === q.answer;
    if (correct) {
      this.setData({ correctCount: (this.data.correctCount || 0) + 1 });
    }

    // **方案二**：为每个选项计算 statusClass
    q.optionsArray = q.optionsArray.map(opt => {
      let cls = '';
      if (opt.key === q.answer) {
        cls = 'correct';
      } else if (opt.key === q.userAnswer) {
        cls = 'wrong';
      }
      return { ...opt, statusClass: cls };
    });

    const answered = [...new Set([...(this.data.answeredQuestions), idx])];
    this.setData({
      questions: this.data.questions,
      showResult: true,
      answeredQuestions: answered
    });

    // 自动下一题
    if (correct) {
      setTimeout(() => this.nextQuestion(), 800);
    }
  },

  // 文本题输入
  onInputAnswer(e) {
    this.setData({ userAnswer: e.detail.value });
  },

  // 文本题提交
  submitTextAnswer() {
    const idx = this.data.currentIndex;
    const q   = this.data.questions[idx];
    if (q.userAnswer) return;

    const val     = (this.data.userAnswer || '').trim();
    q.userAnswer  = val;
    const correct = val === (q.answer || '').trim();
    if (correct) {
      this.setData({ correctCount: (this.data.correctCount || 0) + 1 });
    }

    // 文本题不需要 statusClass 样式
    const answered = [...new Set([...(this.data.answeredQuestions), idx])];
    this.setData({
      questions: this.data.questions,
      showResult: true,
      answeredQuestions: answered
    });

    if (correct) {
      setTimeout(() => this.nextQuestion(), 800);
    }
  },

  // 切换答题卡显隐
  toggleNavigator() {
    this.setData({ showNavigator: !this.data.showNavigator });
  },

  // 点击题号跳转
  goToQuestion(e) {
    const idx = e.currentTarget.dataset.index;
    this.setData({
      currentIndex: idx,
      showResult: false,
      showNavigator: false
    });
  },

  // 下一题
  nextQuestion() {
    const ni = this.data.currentIndex + 1;
    if (ni < this.data.questions.length) {
      this.setData({
        currentIndex: ni,
        showResult: false
      });
    } else {
      wx.showToast({ title: '已经是最后一题了', icon: 'none' });
    }
  }
});
